package io.ede.modules.ede.controller;

import java.util.*;

import io.ede.common.validator.ValidatorUtils;
import io.ede.modules.ede.entity.ReplyEntity;
import io.ede.modules.ede.service.ReplyService;
import io.ede.modules.sys.entity.SysUserEntity;
import io.ede.modules.sys.service.SysUserService;
import io.ede.modules.sys.shiro.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.ede.modules.ede.entity.MessageEntity;
import io.ede.modules.ede.service.MessageService;
import io.ede.common.utils.PageUtils;
import io.ede.common.utils.R;


/**
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2019-08-27 13:28:08
 */
@RestController
@RequestMapping("ede/message")
public class MessageController {
    @Autowired
    private MessageService messageService;
    @Autowired
    private ReplyService replyService;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("ede:message:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = messageService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("ede:message:info")
    public R info(@PathVariable("id") Integer id) {
        MessageEntity message = messageService.getById(id);

        return R.ok().put("message", message);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("ede:message:save")
    public R save(@RequestBody MessageEntity message) {
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        message.setUserId(userEntity.getUserId().intValue());
        message.setCreateTime(new Date());
        messageService.save(message);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("ede:message:update")
    public R update(@RequestBody MessageEntity message) {
        ValidatorUtils.validateEntity(message);
        messageService.updateById(message);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("ede:message:delete")
    public R delete(@RequestBody Integer[] ids) {
        messageService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 查询所有的留言和回复
     * @return
     */
    @RequestMapping("/getAllList")
    public R getAllList() {
        List<MessageEntity> messageList = (List<MessageEntity>) messageService.listByMap(new HashMap<>());
        for (int i = 0; i < messageList.size(); i++) {
            messageList.get(i).setUserName(sysUserService.getById(messageList.get(i).getUserId()).getUsername());
            Map<String,Object> params = new HashMap<>();
            params.put("message_id",messageList.get(i).getId());
            List<ReplyEntity> replyEntityList = (List<ReplyEntity>)replyService.listByMap(params);
            messageList.get(i).setReplyEntityList(replyEntityList);
        }
        return R.ok().put("messageList", messageList);
    }

    @RequestMapping("/reply")
    @RequiresPermissions("ede:message:reply")
    public R reply(@RequestBody MessageEntity message) {
       ReplyEntity replyEntity = new ReplyEntity();
       replyEntity.setMessageId(message.getId());
       replyEntity.setReply(message.getReplyContent());
       replyEntity.setCreateTime(new Date());
       replyService.save(replyEntity);
       return R.ok();
    }

}
